﻿@{
    ViewData["Title"] = "车辆参数主表";
    Layout = "_Layout_DisplayAll";
}


<table id="T_VehicleMaster" lay-filter="T_VehicleMaster" lay-even></table>


@section Scripts{


    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">


            <button class="layui-btn layui-btn-sm" lay-event="AddData">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="ReloadData">刷新</button>
        </div>
    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="Singeldel">删除</a>
    </script>

    <script>
        var user = "@ViewBag.User";

        layui.use(['element', 'layer', 'table', 'form'], function () {
            var element = layui.element;
            var table = layui.table;
            var $ = layui.jquery;
            var bodyAdd;
            var bodyEdit;
            var tableIns = table.render(
                {
                    elem: '#T_VehicleMaster'
                    , toolbar: '#toolbarDemo'
                    , url: '@Url.Action("GetT_VehicleMasterSetup", "Home")' //数据接口
                    , limit: 30
                    , even: true
                    , page: true //开启分页
                    , size: 'sm'
                    , cols: [[ //表头

                        { field: 'id', title: 'id', sort: true }

                        , { field: 'vehicleid', title: '车辆编号',  sort: true }
                        , { field: 'state', title: '显示权限',  sort: true }
                        
                        , { field: 'samplerate', title: '采样率',  sort: true }
                        , { field: 'numberpoints', title: '数据点数',  sort: true }
                        , { field: 'importaccess', title: '导入权限',  sort: true }
                        , { field: 'analysisaccess', title: '分析权限',  sort: true }
                        , { field: 'predictaccess', title: '预测权限',  sort: true }
                        , { field: 'displaygpspoints', title: 'gps展示点数',  sort: true }
                        , { field: 'area', title: '区域', sort: true }
                        , { field: 'country', title: '国家',  sort: true }
                        , { field: 'remarks', title: '监控标题', sort: true }

                        , { fixed: 'right', title: '操作', width: '120', toolbar: '#barDemo' }
                    ]]
                });

            table.on('tool(T_VehicleMaster)', function (obj) {
                var data = obj.data;
                //console.log(data);
                if (user == "Admin") {
                    if (obj.event === 'edit') {

                        layer.open({
                            type: 2,
                            title: '编辑车辆信息',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['720px', '80%'],
                            content: '@Url.Action("T_VehicleMaster_PopUp", "PopUp")', //iframe的url
                            btn: ['确认', '取消'],
                            yes: function (index) {
                                var vehicleidtext = bodyEdit.find('#vehicleid').val();
                                var areatext = bodyEdit.find('#area').val();
                                var id = data.id;//获得选中行的id值
                                var countrytext = bodyEdit.find('#country').val();
                                var remarkstext = bodyEdit.find('#remarks').val();
                                var samplerate = bodyEdit.find('#samplerate').val();
                                var numberpoints = bodyEdit.find('#numberpoints').val();
                                var displaygpspoints = bodyEdit.find('#displaygpspoints').val();
                                if (vehicleidtext != "" && areatext != "" && countrytext != "" && remarkstext != "" 
                                    && samplerate != "" && numberpoints != "" && displaygpspoints != "") {
                                    $.ajax({
                                        //请求方式
                                        type: "POST",
                                        //请求的媒体类型
                                        //contentType: "application/json;charset=UTF-8",
                                        //请求地址
                                        url: '@Url.Action("AddorEditSingleT_VehicleMaster", "Home")',
                                        //数据，json字符串
                                        data: {
                                            id: id,
                                            vehicleidtext: vehicleidtext,
                                            countrytext: countrytext,
                                            remarkstext: remarkstext,
                                            areatext: areatext,
                                            samplerate: samplerate,
                                            numberpoints: numberpoints,
                                            displaygpspoints: displaygpspoints,
                                            importaccesscheck: iframeEdit.importaccesscheck ? 1 : 0,
                                            analysisaccesscheck: iframeEdit.analysisaccesscheck ? 1 : 0,
                                            predictaccesscheck: iframeEdit.predictaccesscheck ? 1 : 0,
                                            statecheck: iframeEdit.statecheck ? 1 : 0, //显示权限
                                            method: "edit"
                                        },
                                        //请求成功
                                        success: function (result) {
                                            layer.msg(result + "请刷新页面");
                                            if (data.samplerate != samplerate || data.numberpoints != numberpoints || data.remarks !=remarkstext) {
                                                $.ajax({
                                                    type: "POST",
                                                    url: '@Url.Action("DeleteMonitorMemory", "Home")',
                                                    data: {
                                                        vehicleidtext: data.vehicleid,
                                                    },
                                                    error: function (e) {
                                                        layer.msg(e);
                                                    }
                                                });
                                            }
                                            $.ajax({
                                                type: "POST",
                                                url: '@Url.Action("DeleteOtherMemory", "Home")',
                                                data: {
                                                    vehicleidtext: data.vehicleid,
                                                },
                                                error: function (e) {
                                                    layer.msg(e);
                                                }
                                            });
                                        },

                                        //请求失败，包含具体的错误信息
                                        error: function (e) {
                                            layer.msg(e);
                                        }
                                    });
                                    layer.close(index);
                                }
                                else {
                                    layer.msg('请填写必填项');
                                    //console.log(abscheck);
                                }


                            },
                            success: function (layero, index) {

                                bodyEdit = layer.getChildFrame('body', index);//获得子frame的body里的元素
                                iframeEdit = window[layero.find('iframe')[0]['name']];
                                bodyEdit.find('#vehicleid').val(data.vehicleid);
                                bodyEdit.find('#area').val(data.area);
                                bodyEdit.find('#country').val(data.country);
                                bodyEdit.find('#remarks').val(data.remarks);
                                bodyEdit.find('#samplerate').val(data.samplerate);
                                bodyEdit.find('#numberpoints').val(data.numberpoints);
                                bodyEdit.find('#displaygpspoints').val(data.displaygpspoints);
                                //console.log("master表先执行");
                                if (data.state === 1) {
                                  
                                    bodyEdit.find('#state').prop('checked', true);
                                }
                                else {
                                    
                                    bodyEdit.find('#state').removeAttr('checked');
                                }
                                if (data.importaccess === 1) {
                                    
                                    bodyEdit.find('#importaccess').prop('checked', true);
                                }
                                else {
                                    
                                    bodyEdit.find('#importaccess').removeAttr('checked');
                                }
                                if (data.analysisaccess === 1) {
                                    bodyEdit.find('#analysisaccess').prop('checked', true);
                                }
                                else {
                                    bodyEdit.find('#analysisaccess').removeAttr('checked');
                                }
                                if (data.predictaccess === 1) {
                                    bodyEdit.find('#predictaccess').prop('checked', true);
                                }
                                else {
                                    bodyEdit.find('#predictaccess').removeAttr('checked');
                                }
                            }
                        });

                    }
                    else if (obj.event === 'Singeldel') {

                        layer.confirm('确定删除吗？此操作会删除所有此车辆信息，请谨慎！！！', function (index) {
                            $.ajax({
                                //请求方式
                                type: "POST",
                                //请求的媒体类型
                                //contentType: "application/json;charset=UTF-8",
                                //请求地址
                                url: '@Url.Action("DeleteSingleT_VehicleMaster", "Home")',
                                //数据，json字符串
                                data: {
                                    id: data.id
                                },
                                //请求成功
                                success: function (result) {
                                    layer.msg(result + "请刷新页面");
                                },
                                //请求失败，包含具体的错误信息
                                error: function (e) {
                                    layer.msg(e);
                                }
                            });
                            layer.close(index);
                        });
                    }
                }
                else {
                    window.location.href = "/Home/Error";
                }
            });

            table.on('toolbar(T_VehicleMaster)', function (obj) {
                switch (obj.event) {
                    case 'AddData':
                        if (user == "Admin") {
                            layer.open({
                                type: 2,
                                title: '添加项目信息',
                                shadeClose: true,
                                shade: 0.8,
                                area: ['720px', '80%'],
                                content: '@Url.Action("T_VehicleMaster_PopUp", "PopUp")', //iframe的url
                                btn: ['确认', '取消'],
                                yes: function (index) {
                                    //var body = layer.getChildFrame('body', index);
                                    var vehicleidtext = bodyEdit.find('#vehicleid').val();
                                    var areatext = bodyEdit.find('#area').val();
                                    //var id = data.id;//获得选中行的id值
                                    var countrytext = bodyEdit.find('#country').val();
                                    var remarkstext = bodyEdit.find('#remarks').val();
                                    
                                    var samplerate = bodyEdit.find('#samplerate').val();
                                    var numberpoints = bodyEdit.find('#numberpoints').val();
                                    var displaygpspoints = bodyEdit.find('#displaygpspoints').val();
                                    if (vehicleidtext != "" && areatext != "" && countrytext != "" && remarkstext != ""
                                        && samplerate != "" && numberpoints != "" && displaygpspoints != "") {
                                        $.ajax({
                                            //请求方式
                                            type: "POST",
                                            //请求的媒体类型
                                            //contentType: "application/json;charset=UTF-8",
                                            //请求地址
                                            url: '@Url.Action("AddorEditSingleT_VehicleMaster", "Home")',
                                            //数据，json字符串
                                            data: {

                                                vehicleidtext: vehicleidtext,
                                                countrytext: countrytext,
                                                
                                                remarkstext: remarkstext,
                                                areatext: areatext,
                                                samplerate: samplerate,
                                                numberpoints: numberpoints,
                                                displaygpspoints: displaygpspoints,

                                                importaccesscheck: iframeAdd.importaccesscheck ? 1 : 0,
                                                analysisaccesscheck: iframeAdd.analysisaccesscheck ? 1 : 0,
                                                predictaccesscheck: iframeAdd.predictaccesscheck ? 1 : 0,
                                                statecheck: iframeAdd.statecheck ? 1 : 0,
                                                method: "add"
                                            },
                                            //请求成功
                                            success: function (result) {
                                                layer.msg(result + "请刷新页面");
                                            },
                                            //请求失败，包含具体的错误信息
                                            error: function (e) {
                                                layer.msg(e);
                                            }
                                        });
                                        layer.close(index);
                                    }
                                    else {
                                        layer.msg('请填写必填项');
                                        //console.log(abscheck);
                                    }
                                },
                                success: function (layero, index) {
                                    bodyEdit = layer.getChildFrame('body', index);
                                    iframeAdd = window[layero.find('iframe')[0]['name']];
                                    bodyEdit.find('#samplerate').val(512);
                                    bodyEdit.find('#numberpoints').val(2560);
                                    bodyEdit.find('#displaygpspoints').val(800);
                                    bodyEdit.find('#area').val("亚洲");
                                    bodyEdit.find('#country').val("中国");
                                    bodyEdit.find('#remarks').val("全球路谱智能预测系统");
                                }
                            });
                        }
                        else {
                            window.location.href = "/Home/Error";
                        }
                        break;
                    case 'ReloadData':
                        tableIns.reload({
                            page: { curr: 1, limit: 30 },
                            url: '@Url.Action("GetT_VehicleMasterSetup", "Home")',
                            method: 'post'
                        });
                        break;
                };
            });

        })


    </script>
}